package cn.signalpha.modules.shop.controller.api;

import cn.signalpha.common.annotation.FormRepeatCommitAnnotation;
import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.dto.IdDTO;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.shop.entity.Favorite;
import cn.signalpha.modules.shop.service.FavoriteService;
import cn.signalpha.modules.shop.vo.FavoriteVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/wap/favorite")
@Api(value = "收藏", tags = "收藏")
public class ApiFavoriteController extends BaseController {

	private final FavoriteService favoriteService;

    @GetMapping("/page")
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<FavoriteVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("userId", getUser().getUserId().toString());
        return R.data(favoriteService.selectFavoritePage(Condition.getPage(query), params));
    }

	@PostMapping("/save")
	@FormRepeatCommitAnnotation
	@ApiOperation(value = "收藏", notes = "传商品id")
	public R save(@Valid @RequestBody IdDTO idDTO) {
		Long userId = getUser().getUserId();
		Long careId = favoriteService.getUserFavoriteId(userId, idDTO.getId());
		if(careId > 0){
			return R.data(careId);
		}

		Favorite favorite = new FavoriteVO();
		favorite.setUserId(userId);
		favorite.setGoodsId(Long.valueOf(idDTO.getId()));
		favoriteService.save(favorite);
		return R.data(favorite.getId());
	}

	@PostMapping("/remove")
	@ApiOperation(value = "取消收藏", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(favoriteService.removeByIds(Func.toLongList(ids)));
	}

}
